package com.example.wjy.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.room.entity.MyJoinTeam;
import com.example.superyi.myteam.R;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

/**
 * author: WangJunYan
 * package: com.example.wjy.adapter
 * date: 2020/6/6
 * time: 15:30
 * 显示“我加入的组队”的RecyclerView的适配器，在MyJoinTeamFragment中使用
 */
public class MyJoinTeamAdapter extends RecyclerView.Adapter<MyJoinTeamAdapter.MyJoinTeamViewHolder> {

    /**
     * A list of teams.
     */
    List<MyJoinTeam> teamList;

    int []colors = {0x80B452CD,0x80FF4500,0x80836FFF,0x8098F5FF,0x8066CD00,0x80FFB90F};

    /**
     * Constructs a list of teams.
     *
     * @param teamList the team data from MyJoinTeamViewModel
     */
    public MyJoinTeamAdapter(List<MyJoinTeam> teamList) {
        this.teamList = teamList;
    }

    public void setTeamList(List<MyJoinTeam> teamList) {
        this.teamList = teamList;
    }

    @NonNull
    @Override
    public MyJoinTeamViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.team_item_layout, parent, false);
        return new MyJoinTeamViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyJoinTeamViewHolder holder, int position) {
        MyJoinTeam team = teamList.get(position);
        holder.teamName.setText(team.getTeamName());
        holder.teamType.setText(team.getTeamKind());
        holder.teamDirection.setText(team.getTeamIntro());
        holder.teamCount.setText(String.valueOf(team.getTeamNumOfAll()));
        holder.cv.setBackgroundColor(colors[position % 6]);
    }

    @Override
    public int getItemCount() {
        return teamList.size();
    }

    /**
     * 管理每个item的数据，其中的域目前并无实际含义，只是为了测试，可在布局文件修改后
     * 对应到数据库的具体字段
     */
    static class MyJoinTeamViewHolder extends RecyclerView.ViewHolder {

        TextView teamName;

        TextView teamType;

        TextView teamDirection;

        TextView teamCount;

        CardView cv;
        public MyJoinTeamViewHolder(@NonNull View itemView) {
            super(itemView);
            teamName = itemView.findViewById(R.id.team_name);
            teamType = itemView.findViewById(R.id.team_kind);
            teamDirection = itemView.findViewById(R.id.team_intro);
            teamCount = itemView.findViewById(R.id.team_num_of_all);
            cv = itemView.findViewById(R.id.team_item);
        }
    }
}
